package junit;

import java.util.Date;
import java.util.List;
import java.util.Map;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import com.coo8.common.json.GsonUtil;

public class RedisTest {

	private JedisPoolConfig poolConfig;

    //sit
//	private String ip="10.57.4.108";
//	private Integer port=6379;
	
	//生产
	private String ip="10.58.22.192";
	private Integer port=6279;	
	//old
//	private String ip="10.58.22.190";
//	private Integer port=6377;
	private JedisPool jedisPool;

	private Integer timeOut=50000;
	
	
	public void setTimeOut(Integer timeOut) {
		this.timeOut = timeOut;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public void setPort(Integer port) {
		this.port = port;
	}

	public void setPoolConfig(JedisPoolConfig poolConfig) {
		this.poolConfig = poolConfig;
	}

	public RedisTest() {
	}

	public JedisPool getJedisPool() {
		if (poolConfig == null) {
			poolConfig = new JedisPoolConfig();
		}
		if (port == null || port < 0) {
			port = 6379;
		}
		if (ip == null || "".equals(ip.trim())) {
			throw new IllegalStateException("need ip. please set ip");
		}
		if (timeOut == null || timeOut <= 0) {
			jedisPool = new JedisPool(poolConfig, ip, port);
		} else {
			jedisPool = new JedisPool(poolConfig, ip, port, timeOut);
		}
		return jedisPool;
	}

	public int size(String redisKey) {
		Jedis client = null;
		int result=0;
		try{
			client = getRedis();
			Long len = client.llen(redisKey);
			result=len == null ? 0 : len.intValue();
			returnRedis(client);
		}catch(Exception e){
			e.printStackTrace();
			if (client != null)
				returnBrokenRedis(client);
		}
		return result;
	}
	public void clear(String redisKey) {
		Jedis client = null;
		try{
			client = getRedis();
			client.del(redisKey);                                                  
			returnRedis(client);
		}catch (Exception e) {
			if (client != null)
				returnBrokenRedis(client);
		}
	}

	public void add(String redisKey) {
		Jedis client = null;
		try{
			client = getRedis();
			client.rpush(redisKey, GsonUtil.getJSON(String.class));
			returnRedis(client);
			
		}catch (Exception ex) {
			if (client != null)
				returnBrokenRedis(client);
		}
	}

	public void offer(String redisKey) {
		add(redisKey);
	}

	public String remove(String redisKey) {
		return poll(redisKey);
	}

	public String poll(String redisKey) {
		Jedis client = null;
		String result=null;
		try{
			client = getRedis();
			result=GsonUtil.<String>getObject(client.lpop(redisKey), String.class);
			returnRedis(client);
		}catch (Exception e) {
			if (client != null){
				returnBrokenRedis(client);
			}
			e.printStackTrace();
		}
		return result;
	}
	public Long lpush(String redisKey,String value) {
		Jedis client = null;
		Long result=null;
		try{
			client = getRedis();
			result=client.lpush(redisKey, value);
			returnRedis(client);
		}catch (Exception e) {
			if (client != null){
				returnBrokenRedis(client);
			}
			e.printStackTrace();
		}
		return result;
	}
	public String element(String redisKey) {
		Jedis client = null;
		String result=null;
		try{
			client = getRedis();
			
			List<String> l = client.lrange(redisKey, 0, 0);
			
			if (l.isEmpty()) {
				return null;
			}
			result=GsonUtil.<String>getObject(l.get(0), String.class);
			returnRedis(client);
		}catch (Exception e) {
			if (client != null){
				returnBrokenRedis(client);
			}
			e.printStackTrace();
		}
		return result;
	}

	public String peek(String redisKey) {
		return element(redisKey);
	}
	public String bgrewriteaof(){
		Jedis client = null;
		String result = null;
		try {
			client = getRedis();
			result = client.bgrewriteaof();
			returnRedis(client);
		} catch (Exception e) {
			if (client != null){
				returnBrokenRedis(client);
			}
			e.printStackTrace();
		}
		return result;
	}
	public String hget(String key, String field) {
		Jedis client = null;
		String result = null;
		try {
			client = getRedis();
			result = client.hget(key, field);
			returnRedis(client);
		} catch (Exception e) {
			if (client != null){
				returnBrokenRedis(client);
			}
			e.printStackTrace();
		}
		return result;
	}

	public Map<String, String> hgetAll(String key){
		Jedis client = null;
		Map<String,String> result = null;
		try {
			client = getRedis();
			result = client.hgetAll(key);
			returnRedis(client);
		} catch (Exception e) {
			if (client != null){
				returnBrokenRedis(client);
			}
			e.printStackTrace();
		}
		return result;
	}
	
	public Long hgetAll(String key,String field,long incrValue){
		Jedis client = null;
		Long result = null;
		try {
			client = getRedis();
			result = client.hincrBy(key, field, incrValue);
			returnRedis(client);
		} catch (Exception e) {
			if (client != null){
				returnBrokenRedis(client);
			}
			e.printStackTrace();
		}
		return result;
	}
	
	
	public Long hset(String key,String field,String value){
		Jedis client = null;
		Long result = null;
		try {
			client = getRedis();
			result=client.hset(key, field, value);
			returnRedis(client);
		} catch (Exception e) {
			if (client != null){
				returnBrokenRedis(client);
			}
			e.printStackTrace();		}
		return result;
	}
	
	
	public Long hincrBy(String key,String field,long value){
		Jedis client = null;
		Long result = null;
		try {
			client = getRedis();
			result=client.hincrBy(key, field, value);
			returnRedis(client);
		} catch (Exception e) {
			if (client != null){
				returnBrokenRedis(client);
			}
			e.printStackTrace();
		}
		return result;
	}
	public boolean exists(String key){
		Jedis client = null;
		boolean result = false;
		try {
			client = getRedis();
			result=client.exists(key);
			returnRedis(client);
		} catch (Exception e) {
			if (client != null){
				returnBrokenRedis(client);
			}
			e.printStackTrace();
			if (client != null)
				returnBrokenRedis(client);
		}
		return result;
	}
	public Long incr(String key){
		Jedis client = null;
		long result =0;
		try {
			client = getRedis();
			result=client.incr(key);
			returnRedis(client);
		} catch (Exception e) {
			if (client != null){
				returnBrokenRedis(client);
			}
			e.printStackTrace();
		}
		return result;
	}
	
	@SuppressWarnings("unchecked")
	public void hmset(String key,Map map){
		Jedis client = null;
		try {
			client = getRedis();
			client.hmset(key, map);
			returnRedis(client);
		} catch (Exception e) {
			if (client != null){
				returnBrokenRedis(client);
			}
			e.printStackTrace();
		}
	}
	
	public void set(String key,String value){
		Jedis client = null;
		try {
			client = getRedis();
			client.set(key, value);
			returnRedis(client);
		} catch (Exception e) {
			if (client != null){
				returnBrokenRedis(client);
			}
			e.printStackTrace();
		}
	}
	
	public void expire(String key,int secounds){
		Jedis client = null;
		try {
			client = getRedis();
			client.expire(key, secounds);
			returnRedis(client);
		} catch (Exception e) {
			if (client != null){
				returnBrokenRedis(client);
			}
			e.printStackTrace();
		}
	}
	
	public String get(String key){
		Jedis client = null;
		String str=null;
		try {
			client = getRedis();
			str = client.get(key);
			returnRedis(client);
		} catch (Exception e) {
			if (client != null){
				returnBrokenRedis(client);
			}
			e.printStackTrace();
		}
		return str;
	}
	
	public Jedis getRedis() {
		Jedis client = null;
		client = getJedisPool().getResource();
		
		return client;
	}
	public List<String> getList(String key,long start,long size){
		Jedis client = null;
		List<String> list = null;
		try {
			client = getRedis();
			if (client.exists(key)) {
				list = client.lrange(key, start, (size + start - 1));
			}
			returnRedis(client);
		} catch (Exception e) {
			if (client != null){
				returnBrokenRedis(client);
			}
			e.printStackTrace();
		}
		return list;
	}
	public List<String> getList(String key){
		Jedis client = null;
		List<String> list = null;
		try {
			client = getRedis();
			if (client.exists(key)) {
				list = client.lrange(key , 0 , -1);
			}
			returnRedis(client);
		} catch (Exception e) {
			if (client != null){
				returnBrokenRedis(client);
			}
			e.printStackTrace();
		}
		return list;
	}
	public void returnRedis(Jedis client) {
		if (client != null) {
			getJedisPool().returnResource(client);		}
	}

	public void returnBrokenRedis(Jedis client) {
		if (client != null) {
			if (client != null) {
				getJedisPool().returnBrokenResource(client);
			}
		}
	}
	public static void main(String arg[]){
		RedisTest redisTest=new RedisTest();
		
//		redisTest.lpush("order_promo1010", "1234567");
//		redisTest.lpush("order_promo1010", "4561235");
//		List<String> list = redisTest.getList("ORDERID_promo131111");
		Jedis jedis = redisTest.getRedis();
//		System.out.println(jedis.exists("1234567"));
//		for(String str:list){
//			System.out.println(str);
//		}
		
//		jedis.persist("order_promo1010");
//		jedis.expire("luckpromId_promo131111", (int) (60*60*24*4));
//		System.out.println(jedis.exists("luckpromId_promo130929"));
//		System.out.println(jedis.ttl("luckpromId_promo131008"));
		
//		Jedis jedis = redisTest.getRedis();
//		boolean exists = jedis.exists("MARKERORDERID1310231616488466");
//		System.out.println(exists);
		
//		redisTest.clear("20131029_promo131111_msg");
//		String ii=redisTest.hget("MARKERORDERID", "1310281731184658");
//		redisTest.hincrBy("MARKERORDERID", "1310281731184658", -1);
		String ii=redisTest.hget("luckpromId_promo131111", "endDate");
		System.out.println(ii);
//		redisTest.clear("luckpromId_promo131111");
//		String i=redisTest.hget("luckpromId_promo130801", "ipCount");
//		redisTest.hset("luckpromId_promo130801", "ipCount", "20");
		
//		redisTest.clear("luckpromId_promo130909");
//		redisTest.hset("luckpromId_promo131111", "maxNum", "10000");
//		redisTest.hset("luckpromId_promo131111", "initNum", "10000");
//		redisTest.hset("luckpromId_promo131111", "startDate", "2013-11-17 23:59:59");
//		List<String> test = redisTest.getList("ORDERID_promo131111");
//		if(test!=null)
//		for(String s: test){
//			System.out.println(s);
//		}
//		else
//			System.out.println("test");
//		redisTest.hset("luckpromId_promo131111", "endDate", "2013-11-18 23:59:59");
//		redisTest.hset("luckpromId_promo131111", "initCondition", "1100");
//		redisTest.hset("luckpromId_promo131111", "probability", "all:10000");
//		redisTest.hset("luckpromId_promo131111", "expValue", "45.0");
//		redisTest.hset("luckpromId_promo131111", "initExp", "");
//		redisTest.hset("luckpromId_promo131111", "awardsType", "2");
//		redisTest.hset("luckpromId_promo131111", "ipCount", "300");
//		
//		redisTest.hset("promo_blackList", "127.0.0.1", "blackList");
//		redisTest.clear("20130820_promo_blackList");
//		redisTest.clear("luckpromId_promo130701");
//		long ii=redisTest.hincrBy("activity_4000001876", "sum_num",816);
//		redisTest.clear("20130817_promo130801_12951343389_used");
//		String iii=redisTest.hget("20130820_promo_blackList", "10.57.40.73");
//		System.out.println(ii);
//		System.out.println("11111");
//		redisTest.clear("20130802_promo130801_msg");
//		redisTest.clear("promo130801_firstprize_msg");
//		List <String> bPrizelist=redisTest.getList("*");
//		if (bPrizelist!=null && !bPrizelist.isEmpty()) {
//			for(String msg:bPrizelist){
//				System.out.println(msg);
//			}
//		}
		
//		redisTest.lpush("promo130801_firstprize_msg","xkphyhua_iPhone5%E4%B8%80%E5%8F%B0");
//		redisTest.lpush("promo130801_firstprize_msg","awen8013_iPhone5%E4%B8%80%E5%8F%B0");
//		redisTest.expire("promo130801_firstprize_msg", 60*60*48);
//		System.out.println("11");
//		redisTest.clear("20130813_promo130809_1441_used");
//		"20130802_promo130801_12383441447_type_1";
//		String ss=redisTest.get("20130808_promo130801_739427_type_4");
//		String ss=redisTest.get("20130802_promo130801_msg");
//		System.out.println(ss);
//		redisTest.clear("20130807_promo130801_739427_type_4");
//		new Date())+"_"+promId+"_"+profile.getRepositoryId()+"_type_"+type;
//		redisTest.clear("20130802_promo130801_600_used");
		
	}
	
	public static void orderMarker(String orderId) {
		RedisTest redisTest=new RedisTest();
		Jedis jedis = redisTest.getRedis();
		String keyString="MARKERORDERID"+orderId;
		jedis.set(keyString, orderId);
		jedis.expire(keyString, 60*20);
	}
	public static boolean isMarker(String orderId) {
		RedisTest redisTest=new RedisTest();
		Jedis jedis = redisTest.getRedis();
		return (jedis.get("MARKERORDERID"+orderId)) != null;
	}
	public static void unMarker(String orderId) {
		RedisTest redisTest=new RedisTest();
		Jedis jedis = redisTest.getRedis();
		jedis.del("MARKERORDERID"+orderId);
	}
}
